<template>
    <pageTabelLayout>
        <template #pageHeader>
            <div class="search-form divClass" v-if="headerShow">
                <div>
                    <a-form size="small" :model="pageData" layout="inline">
                        <a-form-item label="领料单号">
                            <a-input allow-clear :style="{ width: '200px' }" class="bacClass" placeholder=""
                                v-model="pageData.lingliaodanhao" @press-enter="huiche" />
                        </a-form-item>
                        <a-form-item label="客户名称">
                            <a-input allow-clear :style="{ width: '200px' }" class="bacClass" placeholder=""
                                v-model="pageData.kname" @press-enter="huiche" />
                        </a-form-item>
                        <a-form-item label="原料编号">
                            <a-input allow-clear :style="{ width: '200px' }" class="bacClass" placeholder=""
                                v-model="pageData.yuanliaobianhao" @press-enter="huiche" />
                        </a-form-item>
                        <a-form-item label="原料名称">
                            <a-input allow-clear :style="{ width: '200px' }" class="bacClass" placeholder=""
                                v-model="pageData.yuanliaomingc" @press-enter="huiche" />
                        </a-form-item>

                        <a-form-item label="开单日期">
                            <a-range-picker  popup-container="html"  :style="{ width: '350px' }" class="bacClass" @select="onSelect"
                                style="width: 15rem; " @press-enter="huiche" @clear="onClear" v-model="pageData.dateList" />
                        </a-form-item>
                    </a-form>
                </div>
                <div class="my-tool-bar">

                    <Toolbar :items="[
                        `${permissionStore.hasPermisson('lldsh.cx') ? '查询' : ''}`,
                        `${permissionStore.hasPermisson('lldsh.dbsq') ? '调拨单申请' : ''}`,
                        `${permissionStore.hasPermisson('lldsh.sh') ? '审核' : ''}`,
                        `${permissionStore.hasPermisson('lldsh.fs') ? '反审' : ''}`,
                        `${permissionStore.hasPermisson('lldsh.bh') ? '驳回' : ''}`,
                        `${permissionStore.hasPermisson('lldsh.sc') ? '删除' : ''}`]" @select="selectCx" @audit="audits"
                        @rejected="rejecteds" @del="dels" @counter="counters" @addAoutSet="addDbFun">
                    </Toolbar>
                </div>
            </div>
            <div class="my-divisders">
                <a-divider orientation="center" :style="{ width: '250px' }">
                    <icon-double-up v-if="headerShow" @click="headerShow = false" :strokeWidth="2" :size="18" />
                    <icon-double-down v-else @click="headerShow = true" :strokeWidth="2" :size="18" />
                </a-divider>
            </div>
        </template>
        <template #pageTabel>
            <!-- <a-table style="height:100%" size="mini" :scrollbar="true" sticky-header stripe :columns="tableColumns2"
                :row-selection="{ showCheckedAll: true, width: 50, onlyCurrent: true }"
                v-model:selected-keys="selectedRowKeys" :loading="table.tableLoading" :data="datas" :pagination="false"
                rowKey="id" :bordered="{ cell: true }">
                <template #auditState="{ record }">
                    <el-tag v-if="record.auditState == 4" type="success">已&nbsp;&nbsp;拆&nbsp;&nbsp;纱</el-tag>
                    <el-tag v-if="record.auditState == 3" type="success">已&nbsp;&nbsp;领&nbsp;&nbsp;料</el-tag>
                    <el-tag v-if="record.auditState == 2"
                        type="danger">&nbsp;&nbsp;驳&nbsp;&nbsp;&nbsp;&nbsp;回&nbsp;&nbsp;</el-tag>
                    <el-tag v-if="record.auditState == 1" type="success">审核通过</el-tag>
                    <el-tag v-if="record.auditState == 0" type="warning">未&nbsp;&nbsp;审&nbsp;&nbsp;核</el-tag>
                </template>
            </a-table> -->
              <AgGridVuesCopys :rowHeight="33" :headerHeight="33" :suppressCopyRowsToClipboard="true" rowSelection="multiple" @grid-ready="gridReady" style="height: 100%" :defaultColDef="defaultColDef"
                class='ag-theme-alpine' :rowData="datas" :columnDefs="tc" :localeText="localeText1"
                @click="clickRowSelection"></AgGridVuesCopys>
        </template>
        <paginationCopys :page-size-options="pagingList1" :total="pagination.total" show-total show-jumper show-page-size @change="PageSize"
            @page-size-change="change1" style="justify-content: center" :page-size="pagination.pageSize"
            :current="pagination.page" />
        <a-drawer ok-text='保存' cancel-text='关闭' :drawer-style="{ 'zIndex': 999 }" :width="800"
            popup-container=".arco-tabs-content" height="90%" :visible="showAdd" placement="bottom" unmountOnClose
            @cancel="sideNo1" @ok="sideOks" :closable="false">
            <template #title>
                {{ llTitel }}
            </template>
            <a-form ref="formRef" :model="dataForms" size="mini" layout="vertical">
                <div>
                    <a-space>
                        <a-form-item label="领料单号">
                            <a-input v-model="dataForms.planMaterialCode" disabled :style="{ width: '280px' }" />
                        </a-form-item>
                        <a-form-item label="开单日期">
                            <a-input v-model="dataForms.createTime" disabled :style="{ width: '280px' }" />
                        </a-form-item>
                        <a-form-item label="织造序号">
                            <a-input v-model="dataForms.weaveCode" disabled :style="{ width: '280px' }" />
                        </a-form-item>
                        <a-form-item label="计划单号">
                            <a-input v-model="dataForms.planOrderCode" disabled :style="{ width: '280px' }" />
                        </a-form-item>
                    </a-space>
                    <a-space>
                        <a-form-item label="客户名称">
                            <a-input v-model="dataForms.customName" disabled :style="{ width: '280px' }" />
                        </a-form-item>
                        <a-form-item label="领料部门">
                            <a-input class="bacClass" v-model="dataForms.planMaterialdepartment"
                                :style="{ width: '280px' }" />
                        </a-form-item>
                        <a-form-item label="领料人">
                            <a-input class="bacClass" v-model="dataForms.planMaterialPerson" :style="{ width: '280px' }" />
                        </a-form-item>
                        <a-form-item label="备注">
                            <a-input class="bacClass" v-model="dataForms.remark" :style="{ width: '280px' }" />
                        </a-form-item>
                    </a-space>
                </div>
            </a-form>
            <a-table size="mini" :scrollbar="true" sticky-header stripe :columns="tableColumns1" :data="dataList"
                :pagination="false" :bordered="{ cell: true }">
                <template #planMaterialWeight="{ record }">
                    <a-input-number :precision="2" v-model="record['planMaterialWeight']"></a-input-number>
                </template>
                <template #firmId="{ record }">
                    <!-- <a-select popup-container="html"  allow-search allow-clear v-model="record['firmId']" @click="getAllActiveCompany"
                            @change="getAllActiveCompanys($event,record)">
                            <a-option v-for="item in gsDatas" :value="item.id">
                                {{ item.typeName }}{{ item.typeName == "" ? "" : "-" }}{{ item.firmName }}
                            </a-option>
                        </a-select> -->
                    <a-input v-model="record['outSouringCompany']" disabled></a-input>
                </template>
                <template #warehouseName="{ record }">
                    <a-input v-model="record['warehouseName']" disabled></a-input>
                </template>
                <template #cz="{ record }">
                    <a-space>
                        <a-button type="primary" size="mini" status="success" @click="kucun(record)">库存</a-button>
                        <a-button size="mini" status="success" @click="xingqs(record)">详情</a-button>
                    </a-space>
                </template>
            </a-table>
        </a-drawer>
    </pageTabelLayout>
    <a-modal :width="1200" v-model:visible="visible" :on-before-ok="handleOk" @cancel="handleCancel">
        <template #title>
            调拨申请
        </template>
        <a-form ref="formRef2" layout="inline" :model="searchData" size="mini">
            <CorporateSectorCascader ref="corPor" :configCascader="configCascader" @changeCascader="changeCascader" />
            <a-form-item label="申请仓库" :rules="[{ required: true, message: '仓库必填' }]" field="warehouseId">
                <a-select popup-container="html"  class="bacClass" :style="{ width: '15rem' }" v-model="searchData.warehouseId" allow-clear>
                    <a-option v-for="item in fhWhList" :value="item.id">
                        {{ item.warehouseName }}
                    </a-option>
                </a-select>
            </a-form-item>
            <a-form-item label="开单日期" :rules="[{ required: true, message: '日期必填' }]" field="orderDate">
                <a-date-picker  popup-container="html"  class="bacClass" v-model="searchData.orderDate" style="width: 200px;" />
            </a-form-item>
            <a-form-item label="备注" field="remark">
                <a-input class="bacClass" v-model="searchData.remark" style="width: 200px;" />
            </a-form-item>
        </a-form>
        <a-table style="height:100%" :scrollbar="true" sticky-header stripe :columns="tableColumns3" :data="data"
            :pagination="false" :bordered="{ cell: true }">
            <template #planMaterialPieceNum="{ record }">
                <a-input-number @change="pNumber(record)" class="bacClass"
                    v-model="record.planMaterialPieceNum"></a-input-number>
            </template>
            <template #planMaterialBulkWeight="{ record }">
                <a-input-number @change="bNumber(record)" class="bacClass"
                    v-model="record.planMaterialBulkWeight"></a-input-number>
            </template>
            <template #planMaterialBulkNum="{ record }">
                <a-input-number @change="buNumber(record)" class="bacClass"
                    v-model="record.planMaterialBulkNum"></a-input-number>
            </template>
            <template #remark="{ record }">
                <a-input class="bacClass" v-model="record.remark" />
            </template>
        </a-table>
    </a-modal>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue"
import Toolbar from '@/components/Toolbar.vue'
import { useTable, useRowKey, useTableColumn, usePagination, useRowSelection } from '@/hooks/table'
import { post } from "@/api/http";
import { tableColumns1 as tableColumns } from '../useTableList'
import { Message, Modal } from "@arco-design/web-vue"
import CorporateSectorCascader from '@/components/corporateSectorCascader/inde.vue'
import WHService from "@/service/wh"

import { localeText } from '@/api/aggridZw'
import usePermission from '@/store/modules/permission'
import dayjs from 'dayjs'
import { pagingList } from '@/api/paging'
const permissionStore = usePermission()
const pagingList1: any = ref(pagingList)
const localeText1: any = ref(localeText)

const defaultColDef: any = {
    sortable: true,
    resizable: true,
    filter: true,
    // flex: 1
}

const gridOption = ref()
const gridReady = (params: any) => {
    gridOption.value = params.api
}

const dataForms: any = reactive({

})

const dataList: any = ref([])
const formRef = ref()
const showAdd = ref(false)
const llTitel = ref()

const sideNo1 = () => {
    for (let item in dataForms) {
        dataForms[item] = ''
    }
    showAdd.value = false
}
const huiche = () => {
    doRefreshs();
}
onMounted(() => {
    doRefreshs();
})
const sideOks = () => {
    try {
        for (let i = 0; i < dataList.value.length; i++) {
            if (dataList.value[i]['detail'].length == 0 || dataList.value[i]['detail'] == undefined || dataList.value[i]['detail'] == null) {
                Message.error('请输入领料数量')
                return
            }
        }
        post({
            url: 'planSingle/planMaterial/insert',
            data: {
                ...dataForms,
                idList: dataList.value[0].idList,
                planMaterData: dataList.value,
            }
        }).then((res: any) => {
            if (res.code == 200) {
                for (let item in dataForms) {
                    dataForms[item] = ''
                }
                Message.success(res.message)
                showAdd.value = false
            } else {
                Message.error(res.message)
            }
        })
    } catch (error) {
        Message.error('请输入领料数量')
        return
    }
}

const pagination = usePagination(doRefresh)

const table = useTable()

let selectedRowKeys = ref([])

function PageSize(val: any) {
    pagination.page = val
    doRefreshs();

}

function change1(val: any) {
    pagination.pageSize = val
    doRefreshs();
}

async function doRefresh() {
    const res = await post({
        url: 'planSingle/planNeed/getPage',
        data: {
            page: pagination.page,
            size: pagination.pageSize,
            deleteState: 0,
            auditState: 0,
        }
    })
    if (res.code == 200) {
        table.handleSuccess(res.data.list);
        pagination.setTotalSize(res.data.total);
    }
}

const selectCx = () => {
    pagination.page = 1
    doRefreshs();
}
const chuangjianlinliaodans = () => {
    if (selectedRowKeys.value.length == 0) {
        Message.error('请勾选数据据')
        return false
    }
    let isDatasArray: any = []
    selectedRowKeys.value.forEach((o: any) => {
        table.dataList.forEach((v: any) => {
            if (o == v.id) {
                isDatasArray.push(v)
            }
        })
    })
    for (var i = 0; i < isDatasArray.length; i++) {
        for (var j = 1; j < isDatasArray.length; j++) {
            if (isDatasArray[i].planOrderCode != isDatasArray[j].planOrderCode) {
                Message.error('请勾选相同领料单')
                return false
            }
            if (isDatasArray[i].machineCode != isDatasArray[j].machineCode) {
                Message.error('请勾选相同机台号')
                return false
            }
        }
    }
    post({
        url: 'planSingle/planMaterial/getData',
        data: isDatasArray
    }).then((res: any) => {
        if (res.code == 200) {
            dataForms.weaveCode = res.data[0].weaveCode
            dataForms.planOrderCode = res.data[0].planOrderCode
            dataForms.customName = res.data[0].customName
            // dataForms.customName
            dataList.value = res.data
            post({
                url: 'planSingle/planMaterial/getAutomaticInfo',
            }).then((resz: any) => {
                if (resz.code == 200) {
                    dataForms.planMaterialCode = resz.data.planMaterialCode
                    dataForms.createTime = resz.data.planDate
                    llTitel.value = '创建领料单'
                    showAdd.value = true
                } else {
                    Message.error(resz.message)
                }
            })
        } else {
            Message.error(res.message)
        }
    })
}

const tableColumns1: any = [
    {
        title: "机台号",
        key: "machineCode",
        dataIndex: "machineCode",
        width: 150,
    },
    {
        title: "楼层",
        key: "storey",
        dataIndex: "storey",
        width: 150,
    },
    {
        title: "坯布编号",
        key: "greyCode",
        dataIndex: "greyCode",
        width: 200,
    },
    {
        title: "坯布名称",
        key: "greyName",
        dataIndex: "greyName",
        width: 450,
    },
    {
        title: "原料编号",
        key: "rawmaterialCode",
        dataIndex: "rawmaterialCode",
        width: 150,
    },
    {
        title: "原料名称",
        key: "rawmaterialName",
        dataIndex: "rawmaterialName",
        width: 150,
    },
    {
        title: "纱牌",
        key: "brandName",
        dataIndex: "brandName",
        width: 150,
    },
    {
        title: "色号",
        key: "colorCode",
        dataIndex: "colorCode",
        width: 150,
    },
    {
        title: "色称",
        key: "colorName",
        dataIndex: "colorName",
        width: 150,
    },
    // {
    //     title: "领料重量",
    //     key: "planMaterialWeight",
    //     dataIndex: "planMaterialWeight",
    //     slotName: 'planMaterialWeight',
    //     width: 150,
    // },
    {
        title: "公司",
        key: "firmId",
        dataIndex: "firmId",
        slotName: 'firmId',
        width: 250,
    },
    {
        title: "仓库",
        key: "warehouseName",
        dataIndex: "warehouseName",
        slotName: 'warehouseName',
        width: 250,
    },
    {
        title: "批次",
        key: "batch",
        dataIndex: "batch",
        width: 150,
    },
    {
        title: '库存整件/件数',
        key: 'pieceNumber',
        dataIndex: 'pieceNumber',
        width: 150,
    },
    {
        title: '库存整件/件重',
        key: 'pieceWeight',
        dataIndex: 'pieceWeight',
        width: 150,
    },
    {
        title: '库存散件/件数',
        key: 'bulkNumber',
        dataIndex: 'bulkNumber',
        width: 150,
    },
    {
        title: '库存散件/散重',
        key: 'bulkWeight',
        dataIndex: 'bulkWeight',
        width: 150,
    },
    {
        title: '库存总数量',
        key: 'weightTotal',
        dataIndex: 'weightTotal',
        width: 150,
    },
    {
        title: "计划重量",
        key: "planMaterialWeight",
        dataIndex: "planMaterialWeight",
        width: 150,
    },
    {
        title: "操作",
        key: "cz",
        dataIndex: "cz",
        slotName: 'cz',
        width: 150,
    }
]
const gsDatas: any = ref([])
// const getAllActiveCompany = () => {
//     post({
//         url: 'org/firmGroup/getFirmInfoByMap',
//         data: {}
//     }).then(res => {
//         if (res.code == 200) {
//             gsDatas.value = res.data
//         }
//     })
// }
const warehouseNameArray: any = ref([])
const getAllActiveCompanys = ($event: any, record: any) => {
    record['warehouseName'] = ''
    record['warehouseId'] = ''
    post({
        url: 'wms/warehouse/getWarehouseByPara',
        data: {
            firmId: $event,
            virtuallyState:0
        }
    }).then((res: any) => {
        if (res.code == 200) {
            warehouseNameArray.value = res.data
            // 原纱仓
            warehouseNameArray.value.forEach((item: any) => {
                if (item.warehouseName == '原纱仓') {
                    record['warehouseName'] = '原纱仓'
                    record['warehouseId'] = item.id
                }
            })
            post({
                url: ''
            })
        }
    })
}
const handleSearchs = (record: any) => {
    if (record['firmId'] == '' || record['firmId'] == undefined || record['firmId'] == null) {
        Message.error('请先选择公司')
        return
    }
}
const visible = ref(false)
const titels: any = ref()
const formRef2: any = ref()
const handleOk = async () => {
    if (searchData.warehouseId == '' || searchData.warehouseId == null) {
        Message.error('仓库必选')
        return false
    }
    if (searchData.orderDate == '' || searchData.orderDate == null) {
        Message.error('仓库必选')
        return false
    }
    const res: any = await post({
        url: "wms/yarnTransferStock/insertYarnReceipt",
        data: {
            warehouseId: searchData.warehouseId,
            orderDate: searchData.orderDate,
            remark: searchData.remark,
            detailList: data.value
        }
    })
    if (res.code === 200) {
        doRefreshs()
        Message.success('成功')
        return true
    } else {
        Message.error(res.message)
        return false
    }
}
const xingqs = (record: any) => {
    titels.value = '库存详情'
    visible.value = true
    data.value = record['detail']
}
const handleCancel = () => {
    visible.value = false
}
const firmNames: any = ref()
const firmIds: any = ref()
const whNames: any = ref()
const pichs: any = ref()
const rawmaterialCodes: any = ref()
const rawmaterialNames: any = ref()
let records: any = {}
const kucun = (record: any) => {
    records = record
    data.value = record['detail']
    titels.value = '库存'
    firmNames.value = record.outSouringCompany
    firmIds.value = record.outSouringCompanyId
    whNames.value = record.warehouseName
    rawmaterialCodes.value = record.rawmaterialCode
    rawmaterialNames.value = record.rawmaterialName
    pichs.value = record.batch
    visible.value = true
}
const total = ref(10);
const page = ref(1);
const size = ref(10);
function PageSizes(val: any) {
    page.value = val
    defuncx();
}
function change1s(val: any) {
    size.value = val
    defuncx();
}
const defuncx = () => {
    post({
        url: "wms/WarehousetypeFieldvalue/getStockByPage",
        data: {
            "page": page.value,
            "size": size.value,
            "warehouseName": whNames.value,
            "warehouseTypeName": '原纱仓',
            "firmId": firmIds.value,
            "batch": pichs.value,
            "rawMaterialCode": rawmaterialCodes.value,
            // "rawMaterialName": rawmaterialNames.value,
            "infoType": 0
        },
    }).then((res: any) => {
        if (res.code == 200) {
            data.value = res.data.list
            total.value = res.data.total
        }
    })
}
const columns: any = ref([
    {
        title: "单据编号",
        key: "orderNo",
        dataIndex: "orderNo",
        width: 150,
    },
    {
        title: "原料编号",
        key: "rawMaterialCode",
        dataIndex: "rawMaterialCode",
        width: 150,
    },
    {
        title: "原料名称",
        key: "rawMaterialName",
        dataIndex: "rawMaterialName",
        width: 150,
    },
    {
        title: "库存整件/件数",
        key: "pieceNumber",
        dataIndex: "pieceNumber",
        width: 150,
    },
    {
        title: "库存整件/件重",
        key: "pieceWeight",
        dataIndex: "pieceWeight",
        width: 150,
    },
    {
        title: "库存散件/件数",
        key: "bulkNumber",
        dataIndex: "bulkNumber",
        width: 150,
    },
    {
        title: "库存散件/散重",
        key: "bulkWeight",
        dataIndex: "bulkWeight",
        width: 150,
    },
    {
        title: "总数量",
        key: "totalNum",
        dataIndex: "totalNum",
        width: 150,
    },
    {
        title: "领料重量",
        key: "planMaterialWeight",
        dataIndex: "planMaterialWeight",
        slotName: 'planMaterialWeight',
        width: 150,
        fixed: 'right'
    },
])
const tableColumns2: any = ref([
    {
        title: "领料单号",
        key: "planMaterialCode",
        dataIndex: "planMaterialCode",
        width: 150,
    },
    {
        title: "客户名称",
        key: "customName",
        dataIndex: "customName",
        width: 150,
    },
    {
        title: "开单日期",
        key: "createTime",
        dataIndex: "createTime",
        width: 200,
    },
    // {
    //     title: "计划单号",
    //     key: "planOrderCode",
    //     dataIndex: "planOrderCode",
    //     width: 150,
    // },
    // {
    //     title: "机台号",
    //     key: "machineCode",
    //     dataIndex: "machineCode",
    //     width: 150,
    // },
    {
        title: "原料编号",
        key: "rawMaterialCode",
        dataIndex: "rawMaterialCode",
        width: 150,
    },
    {
        title: "原料名称",
        key: "rawMaterialName",
        dataIndex: "rawMaterialName",
        width: 150,
    },
    {
        title: "纱牌",
        key: "brandName",
        dataIndex: "brandName",
        width: 150,
    },
    {
        title: '纱批',
        key: 'yarnBatch',
        dataIndex: 'yarnBatch',
        width: 150,
    },
    {
        title: "色号",
        key: "colorCode",
        dataIndex: "colorCode",
        width: 150,
    },
    {
        title: "色称",
        key: "colorName",
        dataIndex: "colorName",
        width: 150,
    },
    {
        title: '可配色称',
        key: "matchColor",
        dataIndex: "matchColor",
        width: 150
    },
    {
        title: "整件个数",
        key: "pieceCount",
        dataIndex: "pieceCount",
        width: 150,
    },
    {
        title: "领料件数",
        key: "planMaterialPieceNum",
        dataIndex: "planMaterialPieceNum",
        width: 150,
    },
    {
        title: "领料件重",
        key: "planMaterialPieceWeight",
        dataIndex: "planMaterialPieceWeight",
        width: 150,
    },
    {
        title: "领料总重量",
        key: "planMaterialWeight",
        dataIndex: "planMaterialWeight",
        width: 150,
        fixed: 'right'
    },
    {
        title: "单位",
        key: "unit",
        dataIndex: "unit",
        width: 150,
    },
    {
        title: '等级',
        key: "level",
        dataIndex: "level",
        width: 150
    },
    {
        title: '加急件数',
        key: "urgentCount",
        dataIndex: "urgentCount",
        width: 150
    },
    {
        title: '库存整件/件数',
        key: 'pieceNumber',
        dataIndex: 'pieceNumber',
        width: 150,
    },
    {
        title: '库存整件/件重',
        key: 'pieceWeight',
        dataIndex: 'pieceWeight',
        width: 150,
    },
    {
        title: '库存散件/件数',
        key: 'bulkNumber',
        dataIndex: 'bulkNumber',
        width: 150,
    },
    {
        title: '库存散件/散重',
        key: 'bulkWeight',
        dataIndex: 'bulkWeight',
        width: 150,
    },
    {
        title: '库存总数量',
        key: 'weightTotal',
        dataIndex: 'weightTotal',
        width: 150,
    },
    {
        title: "审核状态",
        key: "auditState",
        dataIndex: "auditState",
        slotName: 'auditState',
        width: 150,
        fixed: 'right'
    },
    {
        title: '是否加急',
        key: 'urgentState',
        dataIndex: 'urgentState',
        slotName: 'urgentState',
        width: 120,
        fixed: 'right',
    },
])
let tc: any[] = tableColumns2.value.map((o: any) => {

    let obj = {
        headerName: o.title,
        field: o.key,
        pinned: o['fixed']
    }
    if (o.key === 'auditState') {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj["lockPosition"] = "right"
        obj['width'] = 120
        obj['cellRenderer'] = {
            template: `
        <el-tag v-if="b == 4" type="success">已&nbsp;&nbsp;拆&nbsp;&nbsp;纱</el-tag>
                    <el-tag v-if="b == 3" type="success">已&nbsp;&nbsp;领&nbsp;&nbsp;料</el-tag>
                    <el-tag v-if="b == 2" type="danger">&nbsp;&nbsp;驳&nbsp;&nbsp;&nbsp;&nbsp;回&nbsp;&nbsp;</el-tag>
                    <el-tag v-if="b == 1" type="success">审核通过</el-tag>
                    <el-tag v-if="b == 0" type="warning">未&nbsp;&nbsp;审&nbsp;&nbsp;核</el-tag>
        `,
            setup(props: any) {
                return {
                    b: props.params.data.auditState,
                }
            }

        }
    }
    if (o.key == "planState") {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj['cellRenderer'] = {
            template: ` <span v-if="b == 0">自行生产</span>`,
            setup(props: any) {
                return {
                    b: props.params.data.planState,

                }
            }
        }

    }
    if (o.key == 'planMaterialWeight') {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj["lockPosition"] = "right"
        obj['width'] = 120
        obj['cellRenderer'] = {
            template: `
      <a-popover title="需求单明细">
              <a-button :style="{ width: '100px' }" type="outline">{{b}}</a-button>
              <template #content>
                <a-table :columns="columns" :data="c" :pagination="false"/>
              </template>
           </a-popover>
            `,
            setup(props: any) {
                const columns = ref([
                    {
                        title: '需求单号',
                        key: 'planNeedCode',
                        dataIndex: 'planNeedCode',
                        width: 180,
                    },
                    {
                        title: '机台号',
                        key: 'machineCode',
                        dataIndex: 'machineCode',
                        width: 100,
                    },
                    {
                        title: '需求数量',
                        key: 'needPieceNum',
                        dataIndex: 'needPieceNum',
                        width: 100,
                    },
                    {
                        title: '需求重量',
                        key: 'needWeight',
                        dataIndex: 'needWeight',
                        width: 100,
                    },
                ])
                return {
                    columns,
                    b: props.params.data.planMaterialWeight,
                    c: props.params.data.needInfoList
                }
            }
        }
    }
    if (o.key == 'urgentState') {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj['width'] = 120
        obj['cellRenderer'] = {
            template: `
            <a-button v-if="b == 0" status="success" size="mini">正常</a-button>
            <a-button v-if="b == 1" type="primary" status="danger" size="mini">加急</a-button>
            `,
            setup(props: any) {
                return {
                    b: props.params.data.urgentState,
                }
            }
        }
    }
    return obj
})

let a = [{
    showRowGroup: 'hide_def',
    "headerName": "",
    "checkboxSelection": true,
    "headerCheckboxSelection": true,
    "pinned": "left",
    "width": 50,
    resizable: false,
    filter: false,
    sortable: false,
    minWidth: 50,
    maxWidth: 50,
    lockPosition: "left",
}]
tc = a.concat(tc)

const clickRowSelection = () => {
    selectedRowKeys.value = gridOption.value.getSelectedRows().map((res: any) => res.id)
}
const tableColumns3: any = ref([
    {
        title: "客户名称",
        key: "customName",
        dataIndex: "customName",
        width: 150,
    },
    {
        title: "原料编号",
        key: "rawMaterialCode",
        dataIndex: "rawMaterialCode",
        width: 150,
    },
    {
        title: "原料名称",
        key: "rawMaterialName",
        dataIndex: "rawMaterialName",
        width: 150,
    },
    {
        title: "纱牌",
        key: "brandName",
        dataIndex: "brandName",
        width: 150,
    },
    {
        title: '纱批',
        key: 'yarnBatch',
        dataIndex: 'yarnBatch',
        width: 150,
    },
    {
        title: "色号",
        key: "colorCode",
        dataIndex: "colorCode",
        width: 150,
    },
    {
        title: "色称",
        key: "colorName",
        dataIndex: "colorName",
        width: 150,
    },
    {
        title: '可配色称',
        key: "matchColor",
        dataIndex: "matchColor",
        width: 150
    },
    {
        title: '等级',
        key: "level",
        dataIndex: "level",
        width: 150
    },
    {
        title: "领料整件件数",
        key: "planMaterialPieceNum",
        dataIndex: "planMaterialPieceNum",
        slotName: 'planMaterialPieceNum',
        width: 150,
    },
    {
        title: "整件件重",
        key: "planMaterialPieceWeight",
        dataIndex: "planMaterialPieceWeight",
        width: 150,
    },
    {
        title: "领料散件件数",
        key: "planMaterialBulkWeight",
        dataIndex: "planMaterialBulkWeight",
        slotName: 'planMaterialBulkWeight',
        width: 150,
    },
    {
        title: "领料散件件重",
        key: "planMaterialBulkNum",
        dataIndex: "planMaterialBulkNum",
        slotName: 'planMaterialBulkNum',
        width: 150,
    },
    {
        title: "领料总重量",
        key: "planMaterialWeight",
        dataIndex: "planMaterialWeight",
        width: 150,
    },
    {
        title: "单位",
        key: "unit",
        dataIndex: "unit",
        width: 150,
    },
    {
        title: "备注",
        key: "remark",
        dataIndex: "remark",
        slotName: 'remark',
        width: 150,
    },

])
const datas: any = ref([])
async function doRefreshs() {
    console.log(pageData.dateList);

    if (pageData.dateList.length != 0) {
        var startDate = pageData.dateList[0]
        var endDate = pageData.dateList[1]
        let data1 = startDate
        let data2 = endDate
        var formatDate1 = new Date(startDate)
        var formatDate2 = new Date(endDate)
        if (formatDate1 > formatDate2) {
            startDate = data2 ? data2 + ' 00:00:00' : ''
            endDate = data1 ? data1 + ' 23:59:59' : ''
        } else {
            startDate = data1 ? data1 + ' 00:00:00' : ''
            endDate = data2 ? data2 + ' 23:59:59' : ''
        }
    } else {
        startDate = '',
            endDate = ''
    }
    const res = await post({
        url: 'planSingle/planMaterial/getPage',
        data: {
            page: pagination.page,
            size: pagination.pageSize,
            deleteState: 0,
            startDate: startDate,
            endDate: endDate,
            planOrderCode: pageData.lingliaodanhao,
            rawMaterialCode: pageData.yuanliaobianhao,
            rawMaterialName: pageData.yuanliaomingc,
            customName: pageData.kname,
            planMaterialCode: pageData.lingliaodanhao
        }
    })
    if (res.code == 200) {
        datas.value = res.data.list
        pagination.setTotalSize(res.data.total);
    }
}
var list: any = [];
var list2: any = [];
var list3: any = [];
const audits = () => {
    if (selectedRowKeys.value.length == 0) {
        Message.error('请勾选数据')
        return false
    }
    for (let i = 0; i < selectedRowKeys.value.length; i++) {
        var ob = {};
        ob["auditState"] = 1
        ob["id"] = selectedRowKeys.value[i]
        list.push(ob)
    }
    Modal.confirm({
        title: '系统提示',
        content: '是否审核此订单',
        onOk() {
            post({
                url: "planSingle/planMaterial/audit",
                data: list
            }).then((res: any) => {
                if (res.code === 200) {
                    doRefreshs()
                    Message.success('审核通过')
                    selectedRowKeys.value = []
                } else {
                    Message.error(res.message)
                }
            })
        }
    })
}
const rejecteds = () => {
    if (selectedRowKeys.value.length == 0) {
        Message.error('请勾选数据')
        return false
    }
    for (let i = 0; i < selectedRowKeys.value.length; i++) {
        var ob = {};
        ob["auditState"] = 2
        ob["id"] = selectedRowKeys.value[i]
        list2.push(ob)
    }
    Modal.confirm({
        title: '系统提示',
        content: '是否驳回订单',
        onOk() {
            post({
                url: "planSingle/planMaterial/audit",
                // data: auditStateId,
                data: list2
            }).then((res: any) => {
                if (res.code === 200) {
                    doRefreshs()
                    Message.success('订单已驳回')
                    selectedRowKeys.value = []
                } else {
                    Message.error(res.message)
                }
            })
        }
    })
}

const counters = () => {
    if (selectedRowKeys.value.length == 0) {
        Message.error('请勾选数据')
        return false
    }
    for (let i = 0; i < selectedRowKeys.value.length; i++) {
        var ob = {};
        ob["auditState"] = 0
        ob["id"] = selectedRowKeys.value[i]
        list3.push(ob)
    }
    for (let i = 0; i < gridOption.value.getSelectedRows().length; i++) {
        if (gridOption.value.getSelectedRows()[i].outStockPieceNum > 0 || gridOption.value.getSelectedRows()[i].outStockWeight > 0) {
            Message.error('订单已出库')
            return false
            break
        }
    }
    Modal.confirm({
        title: '系统提示',
        content: '是否反审订单',
        onOk() {
            post({
                url: "planSingle/planMaterial/audit",
                // data: auditStateId,
                data: list3
            }).then((res: any) => {
                if (res.code === 200) {
                    doRefreshs()
                    Message.success('订单已反审')
                    selectedRowKeys.value = []
                } else {
                    Message.error(res.message)
                }
            })
        }
    })
}

const dels = () => {
    if (selectedRowKeys.value.length == 0) {
        Message.error('请勾选数据')
        return false
    }

    Modal.confirm({
        title: '系统提示',
        content: '是否删除订单',
        onOk() {
            post({
                url: "planSingle/planMaterial/delete",
                data: selectedRowKeys.value.map((item: any) => {
                    return item
                })
            }).then((res: any) => {
                if (res.code === 200) {
                    doRefreshs()
                    Message.success(res.message)
                    selectedRowKeys.value = []
                } else {
                    Message.error(res.message)
                }
            })
        }
    })
}


//可复制使用
const headerShow = ref(true)

const pageData: any = reactive({
    dropDownDataqszs: {
        startDate: '',
        endDate: ''
    },
    lingliaodanhao: null,
    dateList: []
})
const focusSel = () => {
    mohuchax({ planOrderCode: '' }, 1)
}
const handleSearch = (val: any) => {
    mohuchax({ planOrderCode: val }, 1)
}
const focusSel2 = () => {
    mohuchax({ rawmaterialCode: '' }, 2)
}
const handleSearch2 = (val: any) => {
    mohuchax({ rawmaterialCode: val }, 2)
}
const focusSel3 = () => {
    mohuchax({ rawmaterialName: '' }, 3)
}
const handleSearch3 = (val: any) => {
    mohuchax({ rawmaterialName: val }, 3)
}
async function mohuchax(val: any, state: any) {
    const res = await post({
        url: 'planSingle/planMaterial/getDropValue',
        data: val
    })
    if (res.code == 200) {
        if (state == 1) {
            pageData.lingliaodanhaos = res.data
        }
        if (state == 2) {
            pageData.yuanliaobianhaos = res.data
        }
        if (state == 3) {
            pageData.yuanliaomingcs = res.data
        }
    }
}
const onSelect = (dateString: any) => {
    pageData.dropDownDataqszs = {
        startDate: dateString[0],
        endDate: dateString[1]
    }
}
const onClear = () => {
    pageData.dropDownDataqszs = {}
}
const data = ref([])
const addDbFun = () => {
    if(gridOption.value.getSelectedRows().length == 0){
        Message.error('请勾选数据')
        return false
    }
    data.value = gridOption.value.getSelectedRows()
    for (let item in searchData) {
        if(item == 'orderDate'){
            searchData[item] = dayjs().format('YYYY-MM-DD')
        }else{
            searchData[item] = null
        }
    }
    data.value.forEach((res: any) => {
        nextTick(() => {
            res['planMaterialBulkWeight'] = 0
            res['planMaterialBulkNum'] = 0
            let intNumber = res['planMaterialPieceNum'] - res['pieceNumber']
            if (intNumber <= 0) {
                res['planMaterialPieceNum'] = 0
            } else {
                res['planMaterialPieceNum'] = intNumber
            }
        })
    })
    visible.value = true
}
const searchData: any = reactive({
    warehouseId: null,
    firmId: null,
    orderDate: null,
    remark: null
})
const configCascader = ref<{}>({
    corporateWidth: '15rem', //公司宽度
    sectorWidth: '180px', //部门宽度
    corporateRquired: true, //公司是否必填
    sectorRquired: false, //部门是否必填
    size: 'mini', //选择框大小'mini' | 'small' | 'medium' | 'large'
    corporateName: '申请公司', //公司label
    sectorName: '选择部门', //部门label
    cascaderShow: 'sector', //配置部门不显示
})
const whService = new WHService()
const fhWhList: any = whService.whList
const changeCascader = (data: any) => {
    const { corporateId, sectorId, sectorLabel, corporateLabel } = data
    searchData.warehouseId = ""
    searchData.firmId = corporateId
    whService.getWarhouseListByFirmId(corporateId, '原纱仓')
}
const pNumber = (e: any) => {
    nextTick(() => {
        e['planMaterialWeight'] = e['planMaterialPieceNum'] * e['planMaterialPieceWeight'] + e['planMaterialBulkNum']
    })
}
const bNumber = (e: any) => {
    nextTick(() => {
        e['planMaterialWeight'] = e['planMaterialPieceNum'] * e['planMaterialPieceWeight'] + e['planMaterialBulkNum']
    })
}
const buNumber = (e: any) => {
    nextTick(() => {
        e['planMaterialWeight'] = e['planMaterialPieceNum'] * e['planMaterialPieceWeight'] + e['planMaterialBulkNum']
    })
}
</script>
<style lang="less" scoped>
.bg-container {
    background-color: white;
    padding: 0;
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    form {
        width: 50% !important;
    }

    // .my-divisders{
    //     margin: 0;
    //     padding: 0;
    // }
    &>.my-header {
        width: 100%;
        padding: 10px;
        background-color: white;
        padding-top: 0;

        // min-height: 10%;
        &>.my-tool-bar {
            text-align: right;
        }
    }
}

.gang {
    background-color: rgb(240, 242, 245);
    height: 10px;
}

.isCxs {
    margin-left: 600px;
    margin-top: 20px;
}

:deep(.ag-cell-value) {
    user-select: initial;
    -moz-user-select: text;
    -ms-user-select: text;
    -webkit-user-select: text;
}

:deep(.arco-table-td-content) {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

:deep(.ag-cell) {
    border-right: 1px solid #dde2eb;
}

:deep(.ag-header-cell) {
    border-right: 1px solid #dde2eb;
}
</style>